import Utils.utils as utils
import os,sys
from django.http import HttpResponse
from django.shortcuts import render
class Editor(utils.Controller):
    def edit(self,request):
        return render(request,"Editor/edit.html")
    def pathlist(self,path):
        test = path
        path = os.path.abspath(os.path.dirname(path) + '\\' + os.path.basename(path))
        files = os.listdir(path)

        folderList = []
        fileList = []
        for i in files:
            isD, fdic = self.filedict(path + '\\' + i)
            if isD:
                folderList.append(fdic)
            else:
                fileList.append(fdic)

        data = {
            "folderList": folderList
            , "fileList": fileList
            , "info": {
                "pathType": "",
                "role": "",
                "id": "",
                "name": ""
            },
            "pathReadWrite": "writeable",
            "thisPath": path.replace('\\','/'),
            "userSpace": {
                "sizeMax": "0",
                "sizeUse": 1024
            }
            , "test": test
        }
        return self.show_json(data)

    def filedict(self,file):
        mode = {
            '0': "---"
            , '1': "--x"
            , '2': "-w-"
            , '3': "-wx"
            , '4': "r--"
            , '5': "r-x"
            , '6': "rw-"
            , '7': "rwx"
        }

        pra = os.stat(file)
        prg = oct(os.stat(file).st_mode)[-3:]
        _file = {
            "name": os.path.basename(file),
            "path": file.replace('\\','/'),
            "mode": mode[prg[0]] + mode[prg[1]] + mode[prg[2]] + " (" + prg + ")",
            "atime": (int)(pra.st_atime),
            "ctime": (int)(pra.st_ctime),
            "mtime": (int)(pra.st_mtime),
            "isReadable": int(prg[0]) in [4, 5, 6, 7],
            "isWriteable": int(prg[0]) in [2, 3, 6, 7],

        }
        if os.path.isdir(file):
            _file.update(
                {
                    "type": "folder"
                    , "isParent": True
                }
            )
            return True, _file
        else:
            _file.update(
                {
                    "type": "file"
                    , "size": os.path.getsize(file)
                    , "isParent": False
                    , "ext": os.path.splitext(file)[-1][1:]
                }
            )
            return False, _file

    def treelist(self,type):
        tree = [
            {
                "name": "收藏夹",
                "ext": "tree-fav",
                "menuType": "menu-tree-fav-root",
                "children": [],
                "path": "{userFav}",
                "type": "folder",
                "open": True,
                "isParent": False
            },
            {
                "name": "我的文档",
                "menuType": "menu-tree-root",
                "ext": "tree-self",
                "children": [
                    {
                        "name": "desktop",
                        "path": "F:\/wamp64\/v-hosts\/kode.localhost\/data\/User\/admin\/home\/desktop\/",
                        "type": "folder",
                        "mode": "drwx rwx rwx(0777)",
                        "atime": 1553181192,
                        "ctime": 1552981284,
                        "mtime": 1553181192,
                        "isReadable": 1,
                        "isWriteable": 1,
                        "isParent": False
                    },
                    {
                        "name": "图片",
                        "path": "F:\/wamp64\/v-hosts\/kode.localhost\/data\/User\/admin\/home\/图片\/",
                        "type": "folder",
                        "mode": "drwx rwx rwx(0777)",
                        "atime": 1552981277,
                        "ctime": 1552981277,
                        "mtime": 1552981277,
                        "isReadable": 1,
                        "isWriteable": 1,
                        "isParent": False
                    },
                    {
                        "name": "我的文档",
                        "path": "F:\/wamp64\/v-hosts\/kode.localhost\/data\/User\/admin\/home\/我的文档\/",
                        "type": "folder",
                        "mode": "drwx rwx rwx(0777)",
                        "atime": 1552981277,
                        "ctime": 1552981277,
                        "mtime": 1552981277,
                        "isReadable": 1,
                        "isWriteable": 1,
                        "isParent": False
                    },
                    {
                        "name": "视频",
                        "path": "F:\/wamp64\/v-hosts\/kode.localhost\/data\/User\/admin\/home\/视频\/",
                        "type": "folder",
                        "mode": "drwx rwx rwx(0777)",
                        "atime": 1553092544,
                        "ctime": 1552981277,
                        "mtime": 1553092544,
                        "isReadable": 1,
                        "isWriteable": 1,
                        "isParent": False
                    },
                    {
                        "name": "音乐",
                        "path": "F:\/wamp64\/v-hosts\/kode.localhost\/data\/User\/admin\/home\/音乐\/",
                        "type": "folder",
                        "mode": "drwx rwx rwx(0777)",
                        "atime": 1552981277,
                        "ctime": 1552981277,
                        "mtime": 1552981277,
                        "isReadable": 1,
                        "isWriteable": 1,
                        "isParent": False
                    }
                ],
                "path": "F:\/wamp64\/v-hosts\/kode.localhost\/data\/User\/admin\/home\/",
                "type": "folder",
                "open": True,
                "isParent": True
            },
            {
                "name": "公共目录",
                "menuType": "menu-tree-group-root menu-tree-group-public",
                "ext": "group-public",
                "children": [
                    {
                        "name": "share",
                        "path": "{groupPath}:1\/share\/",
                        "type": "folder",
                        "mode": "drwx rwx rwx(0777)",
                        "atime": 1552981277,
                        "ctime": 1552981277,
                        "mtime": 1552981277,
                        "isReadable": 1,
                        "isWriteable": 1,
                        "isParent": False
                    },
                    {
                        "name": "图片资料",
                        "path": "{groupPath}:1\/图片资料\/",
                        "type": "folder",
                        "mode": "drwx rwx rwx(0777)",
                        "atime": 1552981277,
                        "ctime": 1552981277,
                        "mtime": 1552981277,
                        "isReadable": 1,
                        "isWriteable": 1,
                        "isParent": False
                    },
                    {
                        "name": "文档",
                        "path": "{groupPath}:1\/文档\/",
                        "type": "folder",
                        "mode": "drwx rwx rwx(0777)",
                        "atime": 1552981277,
                        "ctime": 1552981277,
                        "mtime": 1552981277,
                        "isReadable": 1,
                        "isWriteable": 1,
                        "isParent": False
                    },
                    {
                        "name": "视频资料",
                        "path": "{groupPath}:1\/视频资料\/",
                        "type": "folder",
                        "mode": "drwx rwx rwx(0777)",
                        "atime": 1552981277,
                        "ctime": 1552981277,
                        "mtime": 1552981277,
                        "isReadable": 1,
                        "isWriteable": 1,
                        "isParent": False
                    }
                ],
                "path": "{groupPath}:1\/",
                "type": "folder",
                "open": True,
                "isParent": True
            }

        ]
        return self.show_json(tree)
    def image(self,request,path,time):
       # os.path.splitext(file)[-1][1:]
       try:
            img = open(path,'rb')
            return HttpResponse(img.read(), content_type="image/*")
       except Exception as e:
            return HttpResponse(str(e))
